// pages/reg/reg.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time:60,
    showtime:false,
    tp1:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getinfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  getinfo:function(){
    wx.authorize({
      scope: 'scope.userInfo',
      success() {
        wx.getUserInfo({
          success: function (res) {
            var userInfo = res.userInfo
            var nickName = userInfo.nickName
            var avatarUrl = userInfo.avatarUrl
            var gender = userInfo.gender //性别 0：未知、1：男、2：女
            var province = userInfo.province
            var city = userInfo.city
            var country = userInfo.country

            wx.setStorageSync("userinfo", userInfo);

            console.log(userInfo);
          }
        })

      }
    })
  },

  phone1:function(e){
    console.log(e.detail.value);
    var phone = e.detail.value.replace(/\s/g,'');
    this.setData({
      phone1:phone
    })
  },

  yzm:function(e){
    var yzm = e.detail.value;
    yzm = yzm.replace(/\s/g,'');
    this.setData({
      yzm:yzm
    })

  },
  pwdipt1:function(e){
    var pwd = e.detail.value;
    pwd = pwd.replace(/\s/g,"");
    this.setData({
      pwd1:pwd
    })
  },
  sendyzm:function(){
    var phone = this.data.phone1;
    var reg = /^1[3456789]\d{9}$/;
    var that = this;
    if(this.data.showtime){
      return false;
    }
    if(reg.test(phone)){
      wx.request({
        url: 'https://m.nongjike.cn/NJK/app/findUserName',
        data:{
          USERNAME:phone
        },
        success:function(r){
          var d = r.data;
          console.log(d);
          if(!d.data){//手机号未注册
            wx.request({
              url: 'https://m.nongjike.cn/NJK/app/verifications',
              data: {
                USERNAME: phone,
              },
              success: function (res) {
                var data = res.data;
                console.log(data);
                that.setData({
                  showyzm: true,
                  showtime: true,
                });

                var timer = setInterval(function () {
                  var time = that.data.time;
                  console.log(time);
                  time--;
                  if (time <= 0) {
                    clearInterval(timer);
                  }
                  that.setData({
                    time: (time > 0 ? time : 60),
                    showtime: (time > 0 ? true : false)
                  })
                }, 1000);
              }
            })
          }else{//手机已注册，调至登录账号
            that.setData({
              phone2:phone,
              tp1:false
            });
            return false;
          }
        }
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '请输入合法的手机号码！',
      })
    }
  },

  reg:function(){
    var userinfo = wx.getStorageSync('userinfo');
    var that = this
    if(!userinfo.nickName||!userinfo){
      wx.showModal({
        title: '提示',
        content: '注册信息时需要使用您的微信头像及昵称，请同意获取用户信息！',
        success:function(res){
          console.log(res);
          wx.openSetting({
            success:(res) => {
              console.log(res.authSetting);
              if (!!res.authSetting["scope.userInfo"]){
                console.log("getok");
                that.getinfo();
              }else{

              }
            }
          })
        }
      })
    }else{
      var unionid = wx.getStorageSync("unionid")
      var openid = wx.getStorageSync("openid")
      var nickName = userinfo.nickName
      var avatarUrl = userinfo.avatarUrl
      var gender = userinfo.gender //性别 0：未知、1：男、2：女
      var province = userinfo.province
      var city = userinfo.city
      var country = userinfo.country;
      var phone = this.data.phone1,
      yzm = this.data.yzm,
      pwd = this.data.pwd1;
      var reg1 = /^1[3456789]\d{9}$/;
      if(!reg1.test(phone)){
        wx.showModal({
          title: '提示',
          content: '请输入合法的手机号',
        })
        return false;

      }else if(!pwd||pwd.length<6){
        wx.showModal({
          title: '提示',
          content: '请输入至少6位的密码',
        })
        return false;

      }else if(!yzm){
        wx.showModal({
          title: '提示',
          content: '请输入验证码',
        })
        return false;

      }
      wx.request({
        url: 'https://m.nongjike.cn/NJK/app/XiaoRegister',
        method:"POST",
        header:{
          'content-type':"application/x-www-form-urlencoded"
        },
        data:{
          UNIONID:unionid,
          nickname:nickName,
          sex:gender==1?"男":"女",
          city:city,
          openid:openid,
          headImageUrl:avatarUrl,
          USERNAME:phone,
          PASSWORD:pwd,
          STATUS:2,
          YZM:yzm
        },
        success:function(res){
          var data = res.data;
          if(data.code==2){
            wx.showModal({
              title: '提示',
              content: data.message,
            });
            return false;
          }else if(data.code==1){
            wx.showToast({
              title: '关联成功！',
            })
            wx.setStorageSync("USER_ID", data.USER_ID);
            wx.redirectTo({
              url: '/pages/index/index',
            })
          }else if(data.code==12){
            wx.showModal({
              title: '提示',
              content: data.message,
            });
            return false;
          }else{
            wx.showModal({
              title: '提示',
              content: data.message,
            });
            return false;
          }
        }
      })
    }
  },

  tp1:function(){
    this.setData({
      tp1:true
    })
  },

  tp2: function () {
    this.setData({
      tp1: false
    })
  },

  phone2: function (e) {
    console.log(e.detail.value);
    var phone = e.detail.value.replace(/\s/g, '');
    this.setData({
      phone2: phone
    })
  },
  pwdipt2: function (e) {
    var pwd = e.detail.value;
    pwd = pwd.replace(/\s/g, "");
    this.setData({
      pwd2: pwd
    })
  },

  reg2:function(){
    var phone = this.data.phone2,
    pwd = this.data.pwd2;
    var reg = /^1[345789]\d{9}$/;
    if(!reg.test(phone)){
      wx.showModal({
        title: '提示',
        content: '请输入合法手机号码！',
      });
      return false;
    }else if(!pwd){
      wx.showModal({
        title: '提示',
        content: '请输入密码！',
      });
      return false;
    }

    var userinfo = wx.getStorageSync('userinfo');
    var unionid = wx.getStorageSync("unionid")
    var openid = wx.getStorageSync("openid")
    var nickName = userinfo.nickName
    var avatarUrl = userinfo.avatarUrl
    var gender = userinfo.gender //性别 0：未知、1：男、2：女
    var province = userinfo.province
    var city = userinfo.city
    var country = userinfo.country;

    wx.request({
      url: 'https://m.nongjike.cn/NJK/app/XiaoRegister',
      method: "POST",
      header: {
        'content-type': "application/x-www-form-urlencoded"
      },
      data: {
        UNIONID: unionid,
        nickname: nickName,
        sex: gender == 1 ? "男" : "女",
        city: city,
        openid: openid,
        headImageUrl: avatarUrl,
        USERNAME: phone,
        PASSWORD: pwd,
        STATUS: 1
      },
      success: function (res) {
        var data = res.data;
        console.log(data);
        if (data.code == 2) {
          wx.showModal({
            title: '提示',
            content: data.message,
          });
          return false;
        } else if (data.code == 1) {
          wx.showToast({
            title: '关联成功！',
          })
          wx.setStorageSync("USER_ID", data.USER_ID);
          wx.redirectTo({
            url: '/pages/index/index',
          })
        } else if (data.code == 12) {
          wx.showModal({
            title: '提示',
            content: data.message,
          });
          return false;
        } else {
          wx.showModal({
            title: '提示',
            content: data.message,
          });
          return false;
        }
      }
    })

  }


})